package com.pwx.web.controller;

import com.github.pagehelper.PageInfo;
import com.pwx.dao.EdocTypeMapper;
import com.pwx.entity.Edoc;
import com.pwx.entity.EdocType;
import com.pwx.entity.User;
import com.pwx.services.EdocServices;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * Copyright (C),2021-2022,亚瑟王
 * FileName: EdocController
 * Author: saber
 * Date:2022/10/7 12:02
 */
@Controller
@RequestMapping("/edoc")
public class EdocController {
    @Autowired
    private EdocServices es;
    @Autowired
    private EdocTypeMapper etm;

    @RequestMapping("/getAll")
    public String getAll(@RequestParam Map<String,String> maps, Model model){
        PageInfo<Edoc> info = es.getAll(maps);
        model.addAttribute("info",info);
        return "list";
    }

    @RequestMapping(value = "/getAllType",method = RequestMethod.GET)
    @ResponseBody
    public List<EdocType> getAllType(){
        List<EdocType> edocTypes = etm.findAll();
        return edocTypes;
    }

    @RequestMapping(value = "/addEdoc",method = RequestMethod.POST)
    public String addEdoc(Edoc edoc,Model model){
        boolean f = es.addEdoc(edoc);
        if (f) {
            model.addAttribute("msg","新增成功");
            return "forward:/edoc/getAll";
        }else {
            model.addAttribute("msg","新增失败");
            return "forward:/edoc/getAll";
        }

    }

    @RequestMapping("/delEdoc/{eId}")
    @ResponseBody
    public String delEdoc(@PathVariable("eId")Integer eId){
        if (es.delEdoc(eId)) {
            return "1";
        }
        return "0";
    }

    @RequestMapping("/getEdocByEId/{eId}")
    public String getEdocByEId(@PathVariable("eId")Integer eId,Model model){
        Edoc edoc = es.findEdocByEId(eId);
        model.addAttribute("e",edoc);
        return "update";
    }

    @RequestMapping("/updateEdoc")
    public String updateEdoc(Edoc edoc,Model model){
        boolean f = es.updateEdoc(edoc);
        if (f) {
            model.addAttribute("msg","修改成功");
            return "forward:/edoc/getAll";
        }else {
            model.addAttribute("msg","修改失败");
            return "forward:/edoc/getAll";
        }
    }

} 
